/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sembr.vtk.reader;

import vtk.*;

/**
 *
 * @author Adriano
 */
public class Importer3DS
{
    private vtk3DSImporter importer;
    private vtkPanel panel;
           
    public Importer3DS()
    {
        importer = new vtk3DSImporter();
    }
    
    public Importer3DS(String filePath)
    {
        LoadVTKLibraries();
        importer = new vtk3DSImporter();
        panel = new vtkPanel();
        importer.SetFileName(filePath);     
        read();
        getMappers();
    }
    
    private void read()
    {
        if (!importer.GetFileName().isEmpty())
        {
            importer.ComputeNormalsOn();
            importer.Read();
        }        
else
        {
           System.out.println("3DS File not specified.");
        }
    }
    
    private vtkActorCollection getActors()
    {
        return this.importer.GetRenderer().GetActors();
    }
    
    private void getMappers()
    {
        int nrOfActors = getActors().GetNumberOfItems();
        vtkActor actor = null;
        vtkMapper mapper = new vtkMapper();
        vtkActorCollection actorCollection = getActors();;                
        vtkPolyData poly = new vtkPolyData();
        vtkActor actor2 = null;       
                
        for (int i = 0; i < nrOfActors; ++i)
        {
            actor = (vtkActor)actorCollection.GetItemAsObject(i);
            //double[] color = actor.GetProperty().GetColor();
            mapper = actor.GetMapper();
            vtkScalarsToColors colors = mapper.GetLookupTable();
            mapper.Update();
            poly = (vtkPolyData)mapper.GetInputAsDataSet();           
            //appendPolyData.SetInput(poly);
            //System.out.println(mapper.getClass().getName() + mapper[i].Print());
            vtkPolyDataMapper mapper2 = new vtkPainterPolyDataMapper();
            mapper2.SetInputConnection(poly.GetProducerPort());
            mapper2.SetLookupTable(colors);
            mapper2.ScalarVisibilityOff();
            mapper2.Update();
            actor2 = new vtkActor();     
            actor2.SetMapper(mapper2);
            //actor2.GetProperty().SetColor(color);
            actorCollection.AddItem(actor2);
            panel.GetRenderer().AddActor(actor2);
            
        }       
        
        
        
       //vtkPolyDataMapper polyMapper = new vtkPolyDataMapper();
       //polyMapper.SetInputConnection(appendPolyData.GetOutputPort());
       
       //vtkActor actor2 = new vtkActor();
       //actor2.SetMapper(polyMapper);
       
       
       panel.GetRenderer().SetBackground(255.00, 255.00, 255.00);
    }
    
    public vtkPanel getPanel()
    {
        return panel;
    }
       
   /**
    * @return the importerPanel
    */
//    public VtkImporterPanel getImporterPanel() 
//    {
//        return this.importerPanel;
//    }
//
//    /**
//     * @param importerPanel the importerPanel to set
//     */
//    private void setImporterPanel() 
//    {
//        importerPanel = new VtkImporterPanel();
//        //importerPanel.setRenderer(this.importer.GetRenderer());      
//        
//        vtkActor actor = null;
//        
//        for (int i = 0; i < getActors().GetNumberOfItems(); ++i)
//        {
//            actor = (vtkActor)getActors().GetItemAsObject(i);
//            importerPanel.getImporterRenderer().AddActor(actor);
//        }
//        
//        importerPanel.setCamera(this.importer.GetRenderer().GetActiveCamera());
//        importerPanel.getImporterRenderer().SetBackground(255.00, 255.00, 255.00);        
//        //importerPanel.setRenderWindow();
//    }
//    
    /**
     * laod vtk libraries
     */
    private static void LoadVTKLibraries()
    {
        System.loadLibrary("vtkCommon");
        System.loadLibrary("vtkFiltering");
        System.loadLibrary("vtkGenericFiltering");
        System.loadLibrary("vtkGraphics");
        System.loadLibrary("vtkHybrid");
        System.loadLibrary("vtkImaging");
        System.loadLibrary("vtksys");
        System.loadLibrary("vtkexpat");
        System.loadLibrary("vtkfreetype");
        System.loadLibrary("vtkftgl");
        System.loadLibrary("vtkjpeg");
        System.loadLibrary("vtkzlib");
        System.loadLibrary("vtktiff");
        //System.loadLibrary("vtkMPEG2Encode");
        System.loadLibrary("vtkpng");
        System.loadLibrary("vtkDICOMParser");
        System.loadLibrary("vtkIO");
        System.loadLibrary("vtkRendering");
        System.loadLibrary("vtkVolumeRendering");
        System.loadLibrary("vtkCommonJava");
        System.loadLibrary("vtkFilteringJava");
        System.loadLibrary("vtkGenericFilteringJava");
        System.loadLibrary("vtkGraphicsJava");
        System.loadLibrary("vtkHybridJava");
        System.loadLibrary("vtkImagingJava");
        System.loadLibrary("vtkIOJava");
        System.loadLibrary("vtkGraphicsJava");
        System.loadLibrary("vtkRenderingJava");
        System.loadLibrary("vtkVolumeRenderingJava");
    }  
    
    public static void main(String[] args)
    {
        Importer3DS importer3DS = new Importer3DS("c:\\users\\adriano\\documents\\3ds files\\heart n170111.3ds");               
    }
}


